package com.yuwenxin.dao.impl;

import com.yuwenxin.bean.Category;
import com.yuwenxin.core.BaseDaoImpl;
import com.yuwenxin.core.BaseQueryModel;
import com.yuwenxin.dao.CategoryDao;

import java.sql.ResultSet;
import java.util.HashSet;

public class CategoryDaoimpl extends BaseDaoImpl implements CategoryDao {
    @Override
    public String getSelectAllSql() {
        return "select * from category";
    }

    @Override
    public String getSelectByNameSql() {
        return "select * from category where categoryName = ?";
    }

    @Override
    public String getFuzzyFindSql() {
        return "select * from category where categoryName like ?";
    }

    @Override
    public String getUpdateSql(Object object) {
        return null;
    }

    @Override
    public String getDeleteSql() {
        return "delete from category where idcategory = ?";
    }

    @Override
    public String getSelectByIdSql() {
        return "select * from category where idcategory = ?";
    }

    @Override
    public String getInsertSql(Object object) {
        String categoryName = ((Category) object).getCategoryName();
        StringBuilder sb = new StringBuilder("insert into category (categoryname) values (");
        sb.append(categoryName).append(")");
        return sb.toString();
    }

    @Override
    public String getConditionSql(BaseQueryModel queryModel) {
        return null;
    }

    @Override
    public Object toEntity(ResultSet rs) throws Exception {
        Category category = new Category();
        category.setId(rs.getInt("idcategory"));
        category.setCategoryName(rs.getString("categoryname"));
        category.setMusicSet(new HashSet<>());  // 这里要通过MusicDao来获取一个"select * from music where category_id = ?"的外键查询语句
        return category;
    }
}
